#!/usr/bin/env python
"""
run DA toolchains
Run with (e.g.): python DArun-parallel.py /project-dir [./vectors.txt]
"""


import os
import sys
import datetime
import time
import json
import re
import multiprocessing
import Queue
import threading
import subprocess

mysqlusername = "root"
mysqlpassword = "123456"

header_search_directory = ['/usr/lib/gcc/x86_64-linux-gnu/7/include',
                               '/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed',
'/usr/local/include','/usr/include/x86_64-linux-gnu','/usr/include']

counter=0
total=0


def find_cm_json(directory):
    directory_with_compilecommands = directory
    find = False

    for i in os.walk(directory):
        print(i)
        prefix = i[0] #dir path
        for j in i[2]:
            if j == 'compile_commands.json':
                directory_with_compilecommands = prefix
                find = True
                break
        if(find):
            return directory_with_compilecommands


def getFiles(ccdb):
    cclist=[]
    pathset=set()
    csuffix = '\s*.*\.[cC]\s*'

    for cc in ccdb:
        if not re.match(csuffix,cc['file']):
            continue
        filepath=str(os.path.join(cc['directory'],cc['file']))

        if not os.path.isfile(filepath):
            print (filepath + "not exist\n")
            sys.exit(1)
        if filepath not in pathset:
            pathset.add(filepath)
            cclist.append(filepath)
        
    return cclist



def worker(bin, path, queue):
    global counter
    global total
    while True:
        file = queue.get()
        invocation = [bin, '-p', path, file]
        if len(header_search_directory)!=0:
        	for directorypath in header_search_directory:
            		invocation.append('--extra-arg=-I' + directorypath)
        counter = counter+1
        print(str(counter)+'/'+str(total))
        subprocess.call(invocation)
        queue.task_done()



def DArunparallel(dapath,path,jobs):
    global total
    dapath = os.path.abspath(dapath)
    path = os.path.abspath(path)

    ccfile = open(os.path.join(path, 'compile_commands.json'))
    ccdb = json.load(ccfile)
    ccfile.close()
    cclist = getFiles(ccdb)

    total = len(cclist)

    task_num = jobs
    if task_num > multiprocessing.cpu_count():
        task_num = multiprocessing.cpu_count()

    try:
        queue = Queue.Queue(task_num)
        for i in range(task_num):
            t = threading.Thread(target=worker, args=(dapath, path, queue))
            t.daemon = True
            t.start()
        for entry in cclist:
            queue.put(entry)
        queue.join()
    except KeyboardInterrupt:
        print "\nabort.\n"
        os.kill(0, 9)




if __name__ == '__main__':

    
    directory=sys.argv[1]

    vectorsfile=""
    if(len(sys.argv) == 3):
        vectorsfile=sys.argv[2]
    # else:
    #     print("no vector files in parameter!")
    #     exit()
    
    
    directory_with_compilecommands = find_cm_json(directory)


    Parallelpath = 'python parallel.py'
    DApath = 'bin/DA'
    DATransformerPath = 'bin/DATransformer'
    DADumperPath = 'bin/DADumper'
    

#index the project->dajson

    DArunparallel(DApath,str(directory_with_compilecommands),64)



#initial mysql database
    command = "mysql -u " +mysqlusername + " --password=" + mysqlpassword +" < mysql-dadb-initial.sql" 
    print(command)
    os.system(command)

#merge index and store in mysql database

    start2 =time.time()

    command = DATransformerPath + ' \''+ directory + '\''
    print(command)
    os.system(command)

    end2 =time.time()

#    print('Construct time: %s Seconds'%(end-start))
#    print('Transform time: %s Seconds'%(end2-start2))
#    print('Running time: %s Seconds'%(end2-start))


#dump file dep json file

    if len(vectorsfile) ==0:
        command = DADumperPath + ' \''+ directory + '\''
    else:
        command = DADumperPath + ' \''+ directory + '\''+' \''+ vectorsfile + '\''

    print(command)
    os.system(command)


    command = 'python DABrowser/py2neoc.py'
    print(command)
    os.system(command)

    command = 'python DABrowser/py2neoc_include.py'
    print(command)
    os.system(command)

    command = 'python DABrowser/py2neoc_include_tse.py'
    print(command)
    os.system(command)

    command = 'python DABrowser/py2neoc_symbol_tse.py'
    print(command)
    os.system(command)

    command = 'python DABrowser/py2neoc_function_tse.py'
    print(command)
    os.system(command)


    command = 'python DABrowser/py2neoc_fgv_tse.py'
    print(command)
    os.system(command)


    #end = datetime.datetime.now()

                                  
               
                 
                


